//
//  RootViewController.m
//  MagicalRecord1
//
//  Created by  江志磊 on 15-3-13.
//  Copyright (c) 2015年 江 志磊. All rights reserved.
//

#import "RootViewController.h"
#import "CoreData+MagicalRecord.h"
#import "Person.h"

@interface RootViewController ()
{
}

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSArray *array = @[@"增",@"删",@"改",@"查"];
    for (NSInteger i =0; i<array.count; i++) {
        UIButton *btn  =[UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btn setTitle:array[i] forState:UIControlStateNormal];
        [btn setFrame:CGRectMake(10,74+i*60,300,50)];
        btn.tag =i+100;
        [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }
    // Do any additional setup after loading the view.
}

- (void)btnClicked:(UIButton *)btn{
    switch (btn.tag) {
        case 100:
        {
            //增
            //根据coreData模型创建一个Person对象
            //同时也在指定的数据库
            Person *per = [Person MR_createEntity];
            per.age = [NSNumber numberWithInteger:20];
            per.firstName = @"test1";
            per.lastName = @"test2";
            //将模型通过context保存到指定数据库
            [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
        }
            break;
        case 101:
        {
            //删
            //根据条件获取到要删除的数据
            NSArray *array1 =[Person MR_findByAttribute:@"age" withValue:@20];//@20相当于［NSNumber numberWithInteger：20］自动转换成integer
            for (Person *per in array1) {
                [per MR_deleteEntity];//执行内存删除
            }
            //执行文件删除
            [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
        }
            break;
        case 102:
        {
            //改
            //拿到要修改的目标元素直接更改
            Person *per = [Person MR_findFirst];
            per.firstName = @"user1";
            per.lastName = @"user2";
            per.age = @30;
            //保存
            [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
            
        }
            break;
        case 103:
        {
            //查询
            //获取第一个元素
            Person *per  = [Person MR_findFirst];
            NSLog(@"per name:%@",per.firstName);
            
            //获取所有对象
            NSArray *array  = [Person MR_findAll];
            for (Person *per in array) {
                NSLog(@"per name:%@",per.lastName);
            }
            //查找age为20的person
            NSArray *array1 =[Person MR_findByAttribute:@"age" withValue:@20];
            for (Person *per in array1) {
                NSLog(@"per age:%@",per.age);
            }
        }
            break;
            
        default:
            break;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
